#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdint.h>
#include <assert.h>
#include <math.h>

#include <os/osapi.h>

#include <device/mmioutil.h>
#include <device/sbdc_sb3500.h>
#include <device/sbdspsoc_sb3500.h>
#include <device/sbarmsoc_sb3500.h>

#include <stdio.h>
#include "queue.h"
#include "hs_types.h"
#include "HsTypes.h"
#include "FnThread21.h"
#include "FnThread1.h"
#include "PmdGlobals.h"
#include "wlan_const.h"
#include "wlan_phy_mac.h"
#include "wlan_mac.h"
#include "iqdma_3500.h"
#include "uart_msg.h"
#include "wlan_phy_uart.h"
#include "protocol.h"
#include "timer1.h"
#include "clock.h"
//#include "ad9361.h"
#include "iris404.h"
#include "rf_drv.h"

extern ST_RF_DRV stRfDrv;
extern volatile hs_int32_t rx_latency;
extern volatile unsigned success_count[4];
extern volatile hs_int32_t PayloadLen_plus4;
extern volatile hs_int32_t PayloadLen;

hs_uint8_t TxPayload3500[MAX_PAYLOAD_SIZE];
hs_uint8_t TxPayload3500_ACK[MAX_PAYLOAD_SIZE];
extern int error_count;
extern int success_cnt;
//volatile unsigned Rf_Calibrate_Enable;
//memory monitor
hs_int32_t TX_framecnt=0,Re_TX_framecnt=0,ACK_Tx_framecnt=0;
hs_int32_t RX_framecnt=0,Expect_Rx_framecnt=0,ACK_Rec_framecnt=0,N_Expect_Rx_framecnt=0;
extern hs_int32_t tic1,tic2,tic3,tic4;
hs_int32_t tic5=0,tic6=0;
hs_int32_t Delta_Tx = 0,Delta_Tx_10000 = 0,Delta_Rx = 0;
//memory monitor end

extern hs_uint8_t uart_ctl_msg[UART_CTL_LEN];
extern hs_uint8_t phy2uart_ctl_msg[UART_CTL_LEN];
extern hs_uint8_t uart_payload_msg[MAX_PAYLOAD_SIZE];
hs_uint8_t msg_type,rate_uart;
hs_int32_t len_uart;
hs_int32_t timer_value;
unsigned char  network_payload_layer[]={
	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x00,
	0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
	0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F,
	0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F,
	0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F,
	0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F,
	0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F,
	0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F,
	};
static inline
void local_memcpy(void *to, void *from, hs_int32_t len){
	register hs_int32_t i;
	register hs_uint8_t *to_ch = (hs_uint8_t*)to;
	register hs_uint8_t *from_ch = (hs_uint8_t*)from;

	for(i=0; i<len; i++){
		to_ch[i] = from_ch[i];
	}
}


void form_payload_sb3500(frame *s,int length){
	int i;
	for(i = 0; i < HEADER_BYTES; i++){
		TxPayload3500[0] = s->kind;
		TxPayload3500[1] = s->seq;
		TxPayload3500[2] = s->seq;
		TxPayload3500[3] = s->seq;
		TxPayload3500[4] = s->seq;
		TxPayload3500[5] = s->ack;
		TxPayload3500[6] = s->ack;
		TxPayload3500[7] = s->ack;
		TxPayload3500[8] = s->ack;
	}
	for(i = HEADER_BYTES; i < length; i++){
		TxPayload3500[i] = i;
	}
}

void form_ack_sb3500(frame *s,int length){
	int i;
	for(i = 0; i < HEADER_BYTES; i++){
		TxPayload3500_ACK[0] = s->kind;
		TxPayload3500_ACK[1] = s->seq;
		TxPayload3500_ACK[2] = s->seq;
		TxPayload3500_ACK[3] = s->seq;
		TxPayload3500_ACK[4] = s->seq;
		TxPayload3500_ACK[5] = s->ack;
		TxPayload3500_ACK[6] = s->ack;
		TxPayload3500_ACK[7] = s->ack;
		TxPayload3500_ACK[8] = s->ack;
	}
	for(i = HEADER_BYTES; i < length; i++){
		TxPayload3500_ACK[i] = i;//s->info.data[i-HEADER_BYTES];
	}
}
/* Fetch a packet from the network layer for transmission on the channel. */
void from_network_layer(packet * restrict p,int len){
	int i;
	int frame_num = 0;

	for(i=0;i<len;i++)
	{
		 p->data[i] = network_payload_layer[i + len*frame_num];//net2phy_data_0[i]
	}
	frame_num ++;
}
#if 1
void L1ControlRxTx(hs_int32_t target_speed, hs_int32_t length, hs_int32_t mode)
{
	int i;
	hs_int32_t rx_status;
	//hs_uint8_t tx_flag = 0;
	//hs_uint8_t rx_flag = 0;
	//hs_uint8_t msg_type;
	seq_nr next_frame_to_send; /* 0 or 1 only */
	seq_nr frame_expected; /* 0 or 1 only */
	frame r, s; /* scratch variables */
	//packet buffer; /* current packet being sent */
	//event_type event;
	next_frame_to_send = 0; /* next frame on the outbound stream */
	frame_expected = 0; /* frame expected next */
    // TX Init
	phy_tx_init();
	// RX Init
	phy_rx_init();

	clock_init();
	init_timer1();

	for(i=0; i<length; i++){
		TxPayload3500[i] = i;
	}

	if(mode == 1){
		serial_puts("Enter Transmit mode1!\n");
		stRfDrv.SetFreq(850000000,800000000);
		osFastPause(2500);//2ms
		while(1){
			/** TX **/
			//printf("L1 Tx: Len = %d\n", length);
			Layer1Transmit(target_speed, length+4, TxPayload3500);
			serial_puts("Transmit 1 frame!\n");
			Delta_Tx = tic4-tic3;
			TX_framecnt++;
			if(TX_framecnt == 100)
			{
				//printf("Tx 100000\n");
				serial_puts("Transmit 100 frame!\n");
				osFastPause(25000000);//2ms
				TX_framecnt = 0;
				stRfDrv.SetFreq(800000000,850000000);
				osFastPause(1000);
				return;
				//break;
			}
			osFastPause(25000000);//2ms
			osFastPause(25000000);//2ms
			osFastPause(25000000);//2ms
		}
	}
	else if(mode == 2){
		serial_puts("Enter Receive mode2!\n");
		while(1){
			/** RX **/
			RX_framecnt++;
			rx_status = Layer1Receive(PayloadBytes);
			if(rx_status == HS_COMPLETE){
			//serial_puts("Receive 1 Expected DATA frame!");
				Expect_Rx_framecnt ++;
				if(Expect_Rx_framecnt == 100)
				{
					serial_puts("Receive 10000 frame!\n");
					Expect_Rx_framecnt = 0;
					return;
				}
			    serial_puts_0(PayloadBytes, PayloadLen-4);//PayloadLen-8);
			}
			else{
				N_Expect_Rx_framecnt++;
				serial_puts("Receive 1 Failed frame!\n");
				//serial_puts("Do nothing!\n");
				if(N_Expect_Rx_framecnt == 100)
				{
					serial_puts("No-Receive 10000 frame!\n");
					N_Expect_Rx_framecnt = 0;
					return;
				}
			}
			//*(volatile unsigned *)0x40800000 = count++;
			//osFastPause(1000);
		}
	}
	else if (mode == 5){
		serial_puts("Enter Transmit-Wait-ACK mode5!\n");
		stRfDrv.SetFreq(850000000,800000000);
		osFastPause(2500);//2ms
		//from_network_layer(&buffer,(length-HEADER_BYTES)); /* fetch a packet from the network layer */
		//s.info = buffer; /* prepare to send the initial frame */
		s.kind = data;
		s.seq = next_frame_to_send; /* insert sequence number into frame */
		s.ack = 1 - frame_expected; /* piggybacked ack */
		printf("Transmit %5d: s->seq=%d,s->ack=%d\n",TX_framecnt,s.seq,s.ack);
		serial_puts("Transmit 1 frame!\n");
		form_payload_sb3500(&s,length);
		Layer1Transmit(target_speed, length+4, TxPayload3500);//to_physical_layer(&s); /* transmit the frame */
		Delta_Tx = tic4-tic3;
		TX_framecnt++;
		//osFastPause(250000);
		start_timer1(); /* start the timer running */
		set_timer1_limit(1000000);
		//printf("start_timer\n");
		while(1){
			rx_status = Layer1Receive(PayloadBytes);

			if (rx_status == HS_COMPLETE) { /* a frame has arrived undamaged */
				ACK_Rec_framecnt ++;
				from_physical_layer(&r,(length-HEADER_BYTES)); /* go get it */
				if (r.ack == next_frame_to_send) { /* handle outbound frame stream */
					printf("ACK_rec %5d:r->ack=%d\n",ACK_Rec_framecnt,r.ack);
					serial_puts("Receive 1 ACK frame!\n");
					stop_timer1(); /* turn the timer off */
					//from_network_layer(&buffer,(length-HEADER_BYTES));  /* fetch new pkt from network layer */
					inc(next_frame_to_send); /* invert senders sequence number */
				}
				//s.info = buffer; /* construct outbound frame */
				s.seq = next_frame_to_send; /* insert sequence number into it */
				s.ack = 1 - frame_expected; /* seq number of last received frame */
				printf("Transmit %5d: s->seq=%d,s->ack=%d\n\n",TX_framecnt,s.seq,s.ack);
				serial_puts("Transmit 1 new frame!\n");
				form_payload_sb3500(&s,length);
				osFastPause(250000);
				Layer1Transmit(target_speed, length+4, TxPayload3500);//to_physical_layer(&s); /* transmit the frame */
				Delta_Tx = tic4-tic3;
				start_timer1(); /* start the timer running */
				set_timer1_limit(1000000);
				TX_framecnt++;
				if(TX_framecnt == 1000)
				{
					//printf("Tx 100000\n");
					serial_puts("Transmit 1000 frame!\n");
					TX_framecnt = 0;
					osFastPause(2500000);
					stRfDrv.SetFreq(800000000,850000000);
					osFastPause(1000);//2ms
					//break;
					return;
				}
				//osFastPause(2500000);
			}
			else { //(eventtype == timeout) {
				s.kind = data;
				s.seq = next_frame_to_send; /* insert sequence number into frame */
				s.ack = 1 - frame_expected; /* piggybacked ack */
				form_payload_sb3500(&s,length);
				Layer1Transmit(target_speed, length+4, TxPayload3500);//to_physical_layer(&s); /* transmit the frame */
				Delta_Tx = tic4-tic3;
				Re_TX_framecnt++;
				printf("Re-Transmit:%5d: s->seq=%d,s->ack=%d\n",Re_TX_framecnt,s.seq,s.ack);
				//serial_puts("Transmit timeout!\n");
				serial_puts("Transmit 1 old frame!\n");
				if(Re_TX_framecnt == 1000)
				{
					serial_puts("Re-Transmit 1000 frame!\n");
					osFastPause(2500000);
					Re_TX_framecnt = 0;
					return;
				}
				//osFastPause(2500000);
				start_timer1(); /* start the timer running */
				set_timer1_limit(1000000);
			}
		}
	}
	else if(mode == 3){
		serial_puts("Enter Receive-Send-ACK mode3!\n");
		while(1){
			rx_status = Layer1Receive(PayloadBytes);
			RX_framecnt++;

			if (rx_status == HS_COMPLETE) { /* a frame has arrived undamaged */
				from_physical_layer(&r,(length-HEADER_BYTES)); /* go get it */
				if (r.seq == frame_expected) { /* handle inbound frame stream */
					Expect_Rx_framecnt ++;
					printf("Expect_Rec %5d:r->seq=%d,r->ack=%d\n",Expect_Rx_framecnt,r.seq,r.ack);
					serial_puts("Receive 1 Expected DATA frame!\n");
					if(Expect_Rx_framecnt == 1000)
					{
						serial_puts("Receive 1000 frame!\n");
						Expect_Rx_framecnt = 0;
						return;
					}
					//serial_puts("Save 1 Expected DATA frame!\n");
					//to_network_layer(&r.info,(PayloadLen-HEADER_BYTES)); /* pass packet to network layer */
					//to_arm_layer(PayloadLen-4-HEADER_BYTES); /* pass packet to network layer */
					inc(frame_expected); /* invert seq number expected next */
				}
				s.seq = next_frame_to_send; /* insert sequence number into it */
				s.ack = 1 - frame_expected; /* seq number of last received frame */

				form_ack_sb3500(&s,length);//form_ack_sb3500(&s);
				osFastPause(125000);
				Layer1Transmit(target_speed, length+4, TxPayload3500_ACK);//to_physical_layer(&s); /* transmit the frame */
				Delta_Tx = tic4-tic3;
				ACK_Tx_framecnt++;
				printf("ACK_Transmit %5d:s->ack=%d\n\n",ACK_Tx_framecnt,s.ack);
				serial_puts("Transmit 1 ACK frame!\n");
				}
				/*else{
					N_Expect_Rx_framecnt++;
					printf("No-Expect_Rec %5d:r->seq=%d,r->ack=%d\n",N_Expect_Rx_framecnt,r.seq,r.ack);
					serial_puts("Receive 1 No-Expected DATA frame!\n");
					//serial_puts("Do nothing!\n");
					if(N_Expect_Rx_framecnt == (1000-1))
					{
						serial_puts("No-Receive 10000 frame!\n");
						N_Expect_Rx_framecnt = 0;
						return;
					}
				}*/
			//}

		}
	}
}
#endif

#if 1
void L1ControlMAC()
{
	hs_uint8_t msg;
    hs_uint16_t msg_len;
    hs_int32_t status;
    hs_int32_t tx_len;
    hs_uint8_t *tx_payload;
    hs_uint8_t *p;
    hs_uint8_t mac_msg[16];
    hs_uint8_t *send_pkt_ptr;
	hs_int32_t len;
	hs_int32_t rate;
	hs_int32_t ret;
	//hs_int32_t q_status = 0;

	hs_uint8_t tx_flag = 0;
	hs_uint8_t rx_flag = 0;
	//hs_uint8_t msg_type;

    PayloadLen = 0;

    /** TX Init **/
    phy_tx_init();
    /** RX Init **/
    phy_rx_init();

    //*(volatile unsigned *)0x40800000 = 0;

    while(mac_memory_init_done == 0){
        SPIN(16);
    }

    while(1)
    {
        if(mac_init_done != 0)
        {
        	while(1)
        	{
        		set_timer_limit(1000000);
				timer_value = current_timer_value();
				printf("timer_value = %d\n",timer_value);
        		printf("check_if_queue_full(&Phy2MacQueue)=%d\n",check_if_queue_full(&Phy2MacQueue));
        		printf("check_if_queue_empty(&Phy2MacQueue)=%d\n",check_if_queue_empty(&Phy2MacQueue));
        		while(check_if_queue_full(&Phy2MacQueue)){// || (msg_type == TX_START)){
        			printf("enter while\n");
        			if(check_MAC2PHY_interrupt() == MAC_PHY_INTERRUPT){
						goto START_TX;
					}
					//*(volatile unsigned *)0x40800000 = q_status++;
				}
        		printf("enter rx\n");
        		p = (hs_uint8_t *)enqueue_buffer(&Phy2MacQueue);

#if 1 //### TODO: Move to MAC
                if (mac_config_g.mac_loopback || mac_config_g.rf_disabled)
                {
                    if (p[0] == MAC_PHY_RXEND_INDICATION)
                    {
                        /** Enqueue to PHY to MAC **/
                        /** MAC expects length+CRCLEN(4) **/
                        p[0] = MAC_PHY_RXEND_INDICATION;
                        p[1] = rate_err;
                        p[2] = (PayloadLen) & 0xFF;
                        p[3] = ((PayloadLen) >> 8) & 0xFF;
                        p[4] = NOERROR;
                        enqueue(&Phy2MacQueue);
                    }

	                set_cca_msg_reg(CHANNEL_FREE);

                    continue;
                }
#endif

                /** Receive optimized to process ACK in PHY **/
                status = Layer1Receive(p+5);

                if(status == HS_COMPLETE){

                	/* Returns Packet Type */
                	ret = check_data_or_rts_packet(p+5, &send_pkt_ptr, &len, &rate);

                    /** ACK/CTS Optimization **/
                    if(ret == 0){
                    	printf("ret == 0\n");
                        set_cca_msg_reg(CHANNEL_BUSY);
                        Layer1Transmit(rate, len, send_pkt_ptr);

                        set_cca_msg_reg(CHANNEL_FREE);

                        /** Enqueue to PHY to MAC **/
                        /** MAC expects length+CRCLEN(4) **/
						p[0] = MAC_PHY_RXEND_INDICATION;
						p[1] = rate_err;
						p[2] = (PayloadLen) & 0xFF;
						p[3] = ((PayloadLen) >> 8) & 0xFF;
						p[4] = NOERROR;
						enqueue(&Phy2MacQueue);

                    }
                    else if(ret == 2){
                    	printf("ret == 2\n");
                    	/** Enqueue to PHY to MAC **/
						p[0] = MAC_PHY_RXEND_INDICATION;
						p[1] = rate_err;
						p[2] = (PayloadLen) & 0xFF;
						p[3] = ((PayloadLen) >> 8) & 0xFF;
						p[4] = ACKDESTINED;
						enqueue(&Phy2MacQueue);

                    }
                    else if(ret == 3){
                    	printf("ret == 3\n");
                    	/** Enqueue to PHY to MAC **/
						p[0] = MAC_PHY_RXEND_INDICATION;
						p[1] = rate_err;
						p[2] = (PayloadLen) & 0xFF;
						p[3] = ((PayloadLen) >> 8) & 0xFF;
						p[4] = CTSDESTINED;
						enqueue(&Phy2MacQueue);

                    }
                    /** Check if packet is NOT destined: NAV **/
                    /** Checks if packet is NOT broadcast/multicast **/
                    else if(check_if_packet_notdestined(p+5)){
                    	printf("check_if_packet_notdestined(p+5)\n");
                    	/** Enqueue to PHY to MAC **/
						p[0] = MAC_PHY_RXEND_INDICATION;
						p[1] = rate_err;
						p[2] = (PayloadLen) & 0xFF;
						p[3] = ((PayloadLen) >> 8) & 0xFF;
						p[4] = NOTDESTINED;
						enqueue(&Phy2MacQueue);

                    }
                    else{
						printf("else\n");
						/** Enqueue to PHY to MAC **/
						p[0] = MAC_PHY_RXEND_INDICATION;
						p[1] = rate_err;
						p[2] = (PayloadLen) & 0xFF;
						p[3] = ((PayloadLen) >> 8) & 0xFF;
						p[4] = NOERROR;
						enqueue(&Phy2MacQueue);
                    }

	                set_cca_msg_reg(CHANNEL_FREE);

	                //printf("latency = %d\n", rx_latency);

                    //printf("SUCCESSFUL DECODING OF PACKET (len = %d), delay = %d ", PayloadLen-4, rx_latency/44);
                    //printf("rate = %d\n", rate_err);
                }
                else if (status == HS_ERROR){

                	set_cca_msg_reg(CHANNEL_FREE);

	                //printf("latency = %d\n", rx_latency);

                	printf("DECODING OF PACKET  FAILED(len = %d), delay = %d ",PayloadLen-4, rx_latency/44);
                	printf("rate = %d\n", rate_err);
                }
	            else{

	            	set_cca_msg_reg(CHANNEL_FREE);
	            }

                if(check_MAC2PHY_interrupt() == MAC_PHY_INTERRUPT){
                	goto START_TX;
				}
	        }
        }

        START_TX:
        if(check_MAC2PHY_interrupt() == MAC_PHY_INTERRUPT){

		    msg = mac2phy_msg_location[0];
		    msg_len = mac2phy_msg_location[1] | (mac2phy_msg_location[2] << 8);
		    printf("msg=%d,msg_len=%d\n",msg,msg_len);
		    if(msg_len > 0){
		        local_memcpy((void*)mac_msg, (void*)&mac2phy_msg_location[3], msg_len);
		    }

		    reset_MAC2PHY_interrupt();

		    printf("\n L1: INTERRUPT REQUEST FROM MAC\n");

		    switch(msg)
		    {
		        case MAC_PHY_TXSTART_REQUEST:
		        {
		            printf("\n L1: MAC TXSTART REQUEST\n");

		            rate   = mac_msg[0];
		            tx_len = mac_msg[1] | (mac_msg[2] << 8);

		            tx_payload =    (hs_uint8_t*)((mac_msg[6]) |
		                            (mac_msg[5] << 8) |
		                            (mac_msg[4] << 16) |
		                            (mac_msg[3] << 24));

		            set_cca_msg_reg(CHANNEL_BUSY);
		            //printf("tx_rate=%d,tx_len=%d,tx_payload=%d\n",rate,tx_len,tx_payload);
		            Layer1Transmit(rate, tx_len, tx_payload);
		            TX_framecnt++;
		            osFastPause(125000);

		            if(!check_if_queue_full(&Phy2MacQueue)){
		                p = (hs_uint8_t *)enqueue_buffer(&Phy2MacQueue);
		                p[0] = MAC_PHY_TXSTART_CONFIRM;
		                p[4] = MAC_SUCCESS;
		                enqueue(&Phy2MacQueue);
		            }

		            set_cca_msg_reg(CHANNEL_FREE);
		            break;
		        }

		        case MAC_PHY_CCA_RESET_REQUEST:
		        {
		        	printf("\n L1: MAC PHY CCA RESET REQUEST\n");
		            set_cca_msg_reg(CHANNEL_FREE);
		            break;
		        }

		        case MAC_PHY_CONFIG:
		        {
		        	break;
		        }

		        case MAC_PLME_RESET_REQUEST:
		        {
		        	printf("\n L1: MAC PLME CHARACTERISTICS RESET REQUEST\n");
		        	//init_wlan_phy_mac_variables();
		        	break;
		        }

		        case MAC_PLME_CHARACTERISTICS_REQUEST:
		        {
		        	printf("\n L1: MAC PLME CHARACTERISTICS REQUEST\n");
		            //set_PLME_CHARACTERISTICS();
		            break;
		        }
		    }
        }
    }
}
#endif
#if 0
void
L1ControlLoopback(HS_UINT8 *data, HS_UINT16 len)
{
    HS_UINT8 *p = 0, *data_frame ;

    while(check_if_queue_full(&Phy2MacQueue)){
        ++p ;
    }

    p    = (HS_UINT8 *)enqueue_buffer(&Phy2MacQueue);

    PayloadLen = len ;
    rate_err   = 0 ;

	data_frame = &p[5] ;

    local_memcpy(data_frame, data, len) ;

    if(mac_config_g.pkt_broadcast)
    {
       local_memcpy(&data_frame[FIRST_ADDR_LOCATION], &data[THIRD_ADDR_LOCATION], 6) ;
       local_memcpy(&data_frame[SECOND_ADDR_LOCATION],&data[FIRST_ADDR_LOCATION], 6) ;
       local_memcpy(&data_frame[THIRD_ADDR_LOCATION],&data[SECOND_ADDR_LOCATION], 6) ;
    }
    else
    {
       local_memcpy(&data_frame[SECOND_ADDR_LOCATION], &data[FIRST_ADDR_LOCATION],  6) ;
       local_memcpy(&data_frame[FIRST_ADDR_LOCATION],  &data[SECOND_ADDR_LOCATION], 6) ;
    }

    // change the DS status to "from DS"
    data_frame[1] &= 0xFC ;
    data_frame[1] |= 0x02 ;

    p[0] = MAC_PHY_RXEND_INDICATION ;
}

#endif

